#ifndef HASHDB_MEMORY_MANAGER_H
#define HASHDB_MEMORY_MANAGER_H

/**
 * @file hmm.h
 * @brief hashdb memory manager
 */
#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

/* 说明
 * hmm_enable_resize : 每次调整的幅度是 align(align(size+4, 8), 4096)
*/


#define HMM_BADOFF (-1)   //invalid offset

typedef struct hmm hmm_t;

hmm_t* hmm_new(void* base, uint32_t size);
void hmm_delete(hmm_t* me);
hmm_t* hmm_attach(void* base);
void hmm_detach(hmm_t* me);

int32_t hmm_malloc_off(hmm_t* me, uint32_t size);
void hmm_free_off(hmm_t* me, int32_t off);

void* hmm_malloc(hmm_t* me, uint32_t size);
void hmm_free(hmm_t* me, void* ptr);

void* hmm_ptr(hmm_t* me, int32_t off);
int32_t hmm_off(hmm_t* me, void* ptr);

void* hmm_first_ptr(hmm_t* me);

void hmm_enable_resize(hmm_t* me, int (*resize)(void* ctx, uint32_t newsize), void* ctx);

void hmm_dump(hmm_t* me, FILE* to);

#ifdef __cplusplus
}
#endif
#endif /* HASHDB_MEMORY_MANAGER_H */
